// wtlInterfaceView.h : interface of the CWtlInterfaceView class
//
/////////////////////////////////////////////////////////////////////////////

#pragma once
#include "SoundEffectEditor\WaveTrack.h"


class CWtlInterfaceView : public CWindowImpl<CWtlInterfaceView>, public CDoubleBufferImpl<CWtlInterfaceView>
{
private:
	
		RECT rcGraph;
		WaveTrack& Track;
public:
	DECLARE_WND_CLASS(NULL)

	CWtlInterfaceView(WaveTrack& WTrack) : Track(WTrack) {} ; 
	
	BOOL PreTranslateMessage(MSG* pMsg)
	{
		pMsg;
		return FALSE;
	}

	BEGIN_MSG_MAP(CWtlInterfaceView)
		MESSAGE_HANDLER(WM_CREATE, OnCreate)
		CHAIN_MSG_MAP(CDoubleBufferImpl<CWtlInterfaceView>)
//		MESSAGE_HANDLER(WM_PAINT, OnPaint)

	END_MSG_MAP()

// Handler prototypes (uncomment arguments if needed):
//	LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
//	LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//	LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

	LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		//Draw();
			
		return 0;
	}
//
//	LRESULT OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
//	{
//		CPaintDC dc(m_hWnd);
//		
//		Draw();
//		
		//TODO: Add your drawing code here

//		return 0;
//	} 


	
	void DoPaint(CDCHandle dcMem)
	{
		RECT rect;
		GetClientRect(&rect);
			
		const int xVE = rect.right - rect.left;
		const int yVE = rect.bottom - rect.top;
		const int xWE = xVE;
		const int yWE = yVE;
		const int MaxAmplitude = pow(2, (double)(Track.GetWfx()->Format.wBitsPerSample));
		double nPixPerX = xVE / Track.LengthTrackSec();

	//SetMapMode(hdc, MM_ISOTROPIC);
		//HDC hdc = ::GetDC(m_hWnd);
		//dcMem.SetWindowExt(xWE, yWE);
		//dcMem.SetViewportExt(xVE, -yVE);
		
		//SetWindowExtEx(dcMem, xWE, yWE, NULL);
		//SetViewportExtEx(dcMem, xVE, -yVE, NULL);
	

		HPEN hPen1 = CreatePen(PS_SOLID, 1, RGB(160, 0, 0) );
		//HPEN hOldPen = (HPEN) SelectObject(dcMem, hPen1);
		HPEN hOldPen = dcMem.SelectPen(hPen1);
		HANDLE hFile = Track.GetHandleFile();
		DWORD dwRead;
		short chunk;

		//CMemoryDC dcMem(hdc, &rect);

				
		DWORD Size = Track.GetWaveSize();
		int SizeDivChannels =  Size / Track.GetWfx()->Format.nChannels;
		int nChannels = Track.GetWfx()->Format.nChannels;	
		double nPixPerAmp = yVE / (double)( MaxAmplitude * nChannels) ;

		for(int i = 0; i <= nChannels - 1; i++) {

			DWORD CurrentPosition = Track.GetCurrentPosition();
			
			SetFilePointer( hFile, CurrentPosition + i * sizeof(short), NULL, FILE_BEGIN );
			//SetViewportOrgEx(dcMem, 0,  yVE/(2 * nChannels) +  i * yVE/( nChannels), NULL);
			//dcMem.SetViewportOrg(0, yVE/(2 * nChannels) +  i * yVE/( nChannels));
			
			dcMem.MoveTo(0, yVE/2);
			//MoveToEx(dcMem, 0, 0, NULL);
		
			for(int count = 0; CurrentPosition <= Size; CurrentPosition += sizeof(short)*nChannels ) {
				count+=2 * nChannels;
				ReadFile( hFile, &chunk, sizeof(short), &dwRead, NULL );
				nPixPerX = count * xVE;
				nPixPerX /= SizeDivChannels;
				dcMem.LineTo(nPixPerX , chunk * nPixPerAmp + yVE/2);
				//LineTo(dcMem, nPixPerX , chunk * nPixPerAmp);
			}
		}

		dcMem.SelectPen(hOldPen);
//		return true;

	}



};
